package jm.nj.day10.demo04;

import java.io.*;

/*
* 使用五种方式 拷贝 Demo01.java
* 
* 原始两种
*       一次读写一个字符        一次读写一个数组
* 缓冲流
*       一次读写一个 字符   一次读写一个数组    一次读写一行
* */
public class Demo01 {
    public static void main(String[] args) throws IOException {
        //copy01("Demo01.java","cpy.txt");
        //copy02("Demo01.java","cpy.txt");
        //copy03("Demo01.java","cpy.txt");
        //copy04("Demo01.java","cpy.txt");
        copy05("Demo01.java","cpy.txt");
    }
//缓冲流 一次一行
    private static void copy05(String s, String s1) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(s));
        BufferedWriter bw = new BufferedWriter(new FileWriter(s1));

        String line;
        while ((line = br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        bw.close();
        br.close();
    }

    //缓冲流 一次一个字符数组
    private static void copy04(String s, String s1) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(s));
        BufferedWriter bw = new BufferedWriter(new FileWriter(s1));

        char[] chs = new char[1024];
        int len;
        while ((len = br.read(chs))!=-1){
            bw.write(chs,0,len);
            bw.flush();

        }
        bw.close();
    }

    //缓冲流 一次一个字符
    private static void copy03(String s, String s1) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(s));
        BufferedWriter bw = new BufferedWriter(new FileWriter(s1));

        int read;
        while ((read = br.read())!=-1){
            bw.write((char)read);
            bw.flush();

        }
        br.close();
        bw.close();
    }

    //原始 一次一个字符数组
    private static void copy02(String s, String s1) throws IOException {
        FileReader fr = new FileReader(s);
        FileWriter fw = new FileWriter(s1);

        char[] chs =new char[1024];
        int len;
        while ((len = fr.read(chs))!=-1){
            fw.write(chs,0,len);
            fw.flush();
        }
        fw.close();
        fr.close();
    }

    //原始 一次一个字符
    private static void copy01(String s, String s1) throws IOException {
        FileReader fr = new FileReader(s);
        FileWriter fw = new FileWriter(s1);

        int read;
        while ((read = fr.read())!=-1){
            fw.write((char)read);
            fw.flush();
        }
        fw.close();

    }
}
